/*
 * @Author: Turump 
 * @Date: 2021-03-30 11:13:48 
 * @Last Modified by: Turump
 * @Last Modified time: 2021-03-30 11:15:50
 */

const { ccclass, disallowMultiple, menu } = cc._decorator;

@ccclass
@disallowMultiple()
@menu("屏幕适配/背景图片适配")
export default class BgSizeAdapter extends cc.Component {

    rf: Function;

    setBgScale() {
        if (!this.enabled) {
            return;
        }
        let designSize = cc.view.getVisibleSize();
        let width = this.node.width;
        let height = this.node.height;
        if (height < designSize.height) {
            this.node.setScale(designSize.height / height);
        }
        if (width < designSize.width) {
            this.node.setScale(designSize.width / width);
        }
    }

    onEnable() {
        this.setBgScale();
    }

    onLoad() {
        this.setBgScale();
        this.rf = cc.director.on("ResizeFrame", () => {
            this.setBgScale();
        }, this);
    }

    onDestroy() {
        if (this.rf != null) {
            cc.director.off("ResizeFrame", this.rf, this);
        }
    }
}

